package water.com;

import water.com.DaoSupport.entity.User;
import water.com.util.ConfigurationReader;
import water.com.util.WaterSQLGeneratorImp;
import org.junit.Test;

import java.io.IOException;
import java.sql.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;

public class TestUtil {
    public static void main(String[] args) throws IOException {
        String requestJson="{\n" +
                "    \"sysHead\" : {\n" +
                "    \"serviceId\" : \"0001\",\n" +
                "    \"responseCode\" : \"000\",\n" +
                "    \"currentPage\" : \"0\",\n" +
                "    \"pageSize\" : \"5\",\n" +
                "    \"isLast\" : \"Y\"\n" +
                "    },\n" +
                "    \"body\":{\n" +
                "        \"testFiled\": \"测试报文\"\n" +
                "    }\n" +
                "}";
        int index= requestJson.indexOf("serviceId");
        String temp=requestJson.substring(index);
        int index1 = temp.indexOf(":");
        int index2 = temp.indexOf(",");
        String target=temp.substring(index1+1,index2);
        target = target.trim();
        String serviceId=target.substring(1,5);
        int num = 1;
        Integer one =1;
        num+=one;
        System.out.println(num);

        TreeSet set = new TreeSet();
        set.add(1);
        set.add(90);
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        new Character((char)9);
        System.out.println((char)9);
    }

    @Test
    public void ConfigReaderTest(){
        System.out.println(ConfigurationReader.getInstance().getValueByKey("dataBase", "userName"));
    }

    @Test
    public void subTest(){
        String name = "PAY_SOME_MONEY";
        name = name.toLowerCase();
        while (name.contains("_")){
            int index = name.indexOf("_");
            name = name.substring(0,index)+name.substring(index+1,index+2).toUpperCase()+name.substring(index+2);
        }
        System.out.println(name);
    }

    @Test
    public void enumTest(){
    }
    /**
     * 数据库操作demo
     */
    @Test
    public void TestJDbc(){
        Statement stmt=null;
        ResultSet rs=null;
        Connection conn=null;
        User user =new User();
        user.setId("2");
        String Sql = null;
        WaterSQLGeneratorImp waterSqlGeneratorImp =new WaterSQLGeneratorImp();
        try {
            Sql= waterSqlGeneratorImp.selectGenerator(user);
            System.out.println(Sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/jdbc?&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
            String username="root";
            String password="yanjiadou";
            conn = DriverManager.getConnection(url,username,password);
            stmt= conn.createStatement();
            String sql = Sql;
            rs = stmt.executeQuery(sql);
            System.out.println("id|name|password|email|birthday");

            while(rs.next()) {
                String id = rs.getString("id");
                String name = rs.getString("uname");
                String psw = rs.getString("password");
                String code = rs.getString("code");
                System.out.println(id +" | "+name+" | "+psw+" | "+code+" | ");
            }
        }catch(ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }
        finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                rs = null;
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                stmt = null;

            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn = null;
            }
        }
    }
}
